home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / misc / math / YaYaSieSrc.lha / YaYaSieve / YaYaSieve.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-05  |  3.2 KB  |  158 lines

  1. #define __USE_SYSBASE
  2. #include "carac.h"
  3. #include <proto/dos.h>
  4. #include <proto/exec.h>
  5. #include <exec/execbase.h>
  6. #include <exec/memory.h>
  7. #include <proto/mathieeesingtrans.h>
  8. #include <proto/mathieeesingbas.h>
  9. #include <string.h>
  10.  
  11. #define MEM MEMF_PUBLIC|MEMF_CLEAR
  12. #define TEMPLATE "NUMBER/N,DISPLAY/S,TEST/S"
  13. #define TXT GRAS"\n   YAYASieve 1.3"NORM"\n\n   © Allenbrand Brice 1997\n\n   Ringard'Production\n\n\0$VER: YAYASieve 1.3 ** "__DATE__
  14.  
  15. void Test(unsigned int i);
  16. void Go(unsigned int i,unsigned int k,unsigned char *s);
  17.  
  18. struct DosLibrary *DOSBase;
  19. struct Library *MathIeeeSingTransBase;
  20. struct Library *MathIeeeSingBasBase;
  21. struct ExecBase *SysBase;
  22.  
  23. __saveds int main(void)
  24. {
  25.  struct RDArgs *rdargs;
  26.  int opts[]={0,0,0};
  27.  unsigned int i,j,k;
  28.  unsigned char *s;
  29.  struct DateStamp ds1,ds2;
  30.  
  31.  SysBase=(*(struct ExecBase **)4);
  32.  if(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37))
  33.  {
  34.   if(MathIeeeSingTransBase=(struct Library *)OpenLibrary("mathieeesingtrans.library",0))
  35.   {
  36.    if(MathIeeeSingBasBase=(struct Library *)OpenLibrary("mathieeesingbas.library",0))
  37.    {
  38.     if(rdargs=ReadArgs(TEMPLATE,(long *)opts,NULL))
  39.     {
  40.      if(opts[0])
  41.      {
  42.       i=*((unsigned int *)(opts[0]));
  43.       if(i<10)
  44.        i=10;
  45.       if(opts[2])
  46.        Test(i);
  47.       else
  48.       {
  49.        k=IEEESPFix(IEEESPAdd(IEEESPSqrt(IEEESPFlt(i)),(float)0.5));
  50.        i++;
  51.        i>>=1;
  52.        k++;
  53.        k>>=1;
  54.        if(s=(unsigned char *)AllocVec(((i>>5)+20)<<2,MEM))
  55.        {
  56.         Forbid();
  57.         DateStamp(&ds1);
  58.         Go(i,k,s);
  59.         DateStamp(&ds2);
  60.         Permit();
  61.         Printf("Done\n");
  62.         k=0;
  63.         for(j=i;j;j--)
  64.          if(!((s[j>>3])&(1<<(j&7))))
  65.           k++;
  66.         Printf("%ld primes found\n",k);
  67.         if(opts[1])
  68.         {
  69.          k=0;
  70.          Printf(" %9ld",2);
  71.          for(j=2;j<=i;j++)
  72.           if(!((s[j>>3])&(1<<(j&7))))
  73.           {
  74.            k++;
  75.            if(!(k%7))
  76.             Printf("\n");
  77.            Printf(" %9ld",j+j-1);
  78.           }
  79.         }
  80.         j=(ds2.ds_Minute-ds1.ds_Minute)*3000+(ds2.ds_Tick-ds1.ds_Tick);
  81.         Printf("\n(%ld s %ld ticks)\n\n",j/50,j%50);
  82.         FreeVec(s);
  83.        }
  84.       }
  85.      }
  86.      else
  87.       Printf(TXT);
  88.      FreeArgs(rdargs);
  89.     }
  90.     CloseLibrary(MathIeeeSingBasBase);
  91.    }
  92.    CloseLibrary(MathIeeeSingTransBase);
  93.   }
  94.   CloseLibrary((struct Library *)DOSBase);
  95.  }
  96.  return(0);
  97. }
  98.  
  99. void Go(unsigned int i,unsigned int k,unsigned char *s)
  100. {
  101.  unsigned int j=4,a,b=7,l=25;
  102.  unsigned char buf[]={105,150,52,75,154,37,205,146,102,73,179,164,89,210,44};
  103.  
  104.  *s=32;
  105.  repmem(s+1,&buf[0],15,i/120+1);
  106.  
  107.  for(;j<=k;b+=2,l+=(j<<2),j++)
  108.   if(!(s[j>>3]&(1<<(j&7))))
  109.    for(a=l;a<=i;a+=b)
  110.     s[a>>3]|=(1<<(a&7));
  111. }
  112.  
  113. void Test(unsigned int i)
  114. {
  115.  unsigned int j,k;
  116.  unsigned int *e,*p,pp=1;
  117.  
  118.  if(e=AllocVec(16*8,MEM))
  119.  {
  120.   p=e+16*4;
  121.   Printf("\n%ld=",i);
  122.   k=IEEESPFix(IEEESPSqrt(IEEESPFlt(i)))+1;
  123.   for(j=2;j<=k;j++)
  124.    for(;;)
  125.    {
  126.     if(i%j)
  127.      break;
  128.     i/=j;
  129.     if(e[pp]==j)
  130.      p[pp]++;
  131.     else
  132.     {
  133.      if(e[pp])
  134.       pp++;
  135.      e[pp]=j;
  136.      p[pp]=1;
  137.     }
  138.    }
  139.   if(i!=1)
  140.    if(e[pp]==i)
  141.     p[pp]++;
  142.    else
  143.    {
  144.     if(e[pp])
  145.      pp++;
  146.     e[pp]=i;
  147.     p[pp]=1;
  148.    }
  149.   for(;pp;pp--)
  150.    if(p[pp]!=1)
  151.     Printf("%ld^%ld*",e[pp],p[pp]);
  152.    else
  153.     Printf("%ld*",e[pp]);
  154.   Printf("\b \n\n");
  155.   FreeVec(e);
  156.  }
  157. }
  158.